package com.baozhun.mall.common.util

import com.orhanobut.logger.Logger
import java.util.concurrent.TimeUnit


/**
 * @Title：
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/12/26
 * @Version：1.0.0
 */
object TimeUtils {

    /**
     * 格式化时间为 天 时分秒为->天：：：：
     */
    fun millis2FitTimeSpan(targetMillis: Long, precision: Int): String? {
        if (targetMillis < 0) return ""
        val sb = StringBuilder()
        try {
            var dayMillisUnit = 86400000
            var hoursMillisUnit = 3600000
            var secondMillisUnit = 60000

            var day = TimeUnit.MILLISECONDS.toDays(targetMillis)
            if (precision >= 4 && day > 0)
                sb.append(day.toString() + "天")

            var hours = TimeUnit.MILLISECONDS.toHours(targetMillis - day * dayMillisUnit)
            var hoursStr = if (hours < 10) "0${hours}" else hours
            if (precision >= 3)
                sb.append("${hoursStr}:")

            var minutes = TimeUnit.MILLISECONDS.toMinutes(targetMillis - day * dayMillisUnit - hours * hoursMillisUnit)
            var minutesStr = if (minutes < 10) "0${minutes}" else minutes
            if (precision >= 2)
                sb.append("${minutesStr}:")

            var seconds = TimeUnit.MILLISECONDS.toSeconds(targetMillis - day * dayMillisUnit - hours * hoursMillisUnit - minutes * secondMillisUnit)
            var secondsStr = if (seconds < 10) "0${seconds}" else seconds
            sb.append(secondsStr)
        } catch (e: Exception) {
            Logger.e(e.toString())
        }
        return sb.toString()
    }
}